package com.example.cloud.service.system.impl;

import com.example.cloud.constants.StateConstants;
import com.example.cloud.dao.mongdb.system.MongoAccountRepository;
import com.example.cloud.dao.mongdb.system.MongoUserRepository;
import com.example.cloud.dao.system.AccountRepository;
import com.example.cloud.dao.system.UserRepository;
import com.example.cloud.model.system.Account;
import com.example.cloud.model.system.User;
import com.example.cloud.service.system.AccountService;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.BusinessUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wangtao on 2018/2/3.
 */
@Service
public class AccountServiceImpl implements AccountService {

    private static final Logger logger = Logger.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private MongoAccountRepository mongoAccountRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MongoUserRepository mongoUserRepository;

    @Override
    public Account findOneFromCache(Long id) {
        return accountRepository.findOne(id);
    }

    @Override
    @Transactional
    public void doSave(Account account) {
        // 保存实体
        BusinessUtil.appendRecord(account);
        accountRepository.save(account);
        // 同步缓存
        account = mongoAccountRepository.save(account);
        // 保存用户
        User user = new User();
        user.setUsername(account.getUserName());
        user.setUserLoginName(account.getUserLoginName());
        user.setPassword(DigestUtils.md5Hex(account.getPassword()));
        user.setIsAdmin(account.getAccountType());
        user.setAccountId(account.getId());
        user.setAccountName(account.getAccountName());
        user.setOrganId(account.getOrganId());
        user.setOrganName(account.getOrganName());
        userService.doSave(user);
    }

    @Override
    public void loadCache() {
        List<Account> list = accountRepository.findAll();
        if(!list.isEmpty()){
            mongoAccountRepository.save(list);
            logger.info("*******************账户已全部同步缓存*******************");
        }
    }

    @Override
    @Transactional
    public void doFreeze(List<Long> ids) {
        List<Account> accounts = accountRepository.findByIdIn(ids);
        for (Account account : accounts) {
            account.setStatus(StateConstants.STATUS_FREEZE);
            BusinessUtil.appendRecord(account);
        }
        accountRepository.save(accounts);
        mongoAccountRepository.save(accounts);
        List<User> users = mongoUserRepository.findByAccountIdInAndDeleteFlag(ids,0);
        for (User user : users) {
            user.setStatus(StateConstants.STATUS_FREEZE);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        mongoUserRepository.save(users);
    }

    @Override
    @Transactional
    public void doUse(List<Long> ids) {
        List<Account> accounts = accountRepository.findByIdIn(ids);
        for (Account account : accounts) {
            account.setStatus(StateConstants.STATUS_ACTIVE);
            BusinessUtil.appendRecord(account);
        }
        accountRepository.save(accounts);
        mongoAccountRepository.save(accounts);
        List<User> users = mongoUserRepository.findByAccountIdInAndDeleteFlag(ids,0);
        for (User user : users) {
            user.setStatus(StateConstants.STATUS_ACTIVE);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        mongoUserRepository.save(users);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        List<Account> accounts = accountRepository.findByIdIn(ids);
        for (Account account : accounts) {
            account.setDeleteFlag(1);
            BusinessUtil.appendRecord(account);
        }
        accountRepository.save(accounts);
        mongoAccountRepository.save(accounts);
        List<User> users = mongoUserRepository.findByAccountIdInAndDeleteFlag(ids,0);
        for (User user : users) {
            user.setDeleteFlag(1);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        mongoUserRepository.save(users);
    }
}
